package com.wzq.design.business.base.user.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.wzq.design.base.controller.CommController;
import com.wzq.design.business.base.user.dto.AuthResult;
import com.wzq.design.business.base.user.dto.CaptchaResult;
import com.wzq.design.business.base.user.dto.LoginRequest;
import com.wzq.design.business.base.user.dto.LoginResult;
import com.wzq.design.business.base.user.service.LoginService;
import com.wzq.ssm.parent.base.dto.JsonResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author wangzhiqiang
 * @since 2023/6/29 10:17
 */
@RestController
@RequestMapping("/base/auth")
public class LoginController extends CommController {

    private final LoginService loginService;

    public LoginController(LoginService loginService) {
        this.loginService = loginService;
    }

    @PostMapping("/login")
    public JsonResult<LoginResult> login(@RequestBody @Valid LoginRequest loginRequest) {
        return JsonResult.success("登录成功", loginService.login(loginRequest));
    }

    @PostMapping("/logout")
    public JsonResult<?> logout() {
        StpUtil.logout();
        return JsonResult.success("登出成功");
    }

    /**
     * 获取验证码
     *
     * @author wangzhiqiang
     * @since 2023/7/14 17:21
     */
    @GetMapping("/captcha")
    public JsonResult<CaptchaResult> captcha() {
        return JsonResult.success("验证码获取成功", loginService.captcha());
    }

    /**
     * 获取验证码
     *
     * @author wangzhiqiang
     * @since 2023/7/14 17:21
     */
    @GetMapping("/getAuthData")
    public JsonResult<AuthResult> getAuthData() {
        return JsonResult.success("验证码获取成功", loginService.getAuthData());
    }
}
